// 1、初始化时间插件和富文本插件
function initPlugin() {
    // 初始化日期插件
    jeDate('#articleDate', {
        format: 'YYYY-MM-DD', // 显示格式
        isinitVal: true
    });
    // 初始化富文本插件
    tinymce.init({
        selector: '#articleContent',
        language: 'zh_CN',
    });
}
initPlugin();

//封面图片回显
function showImage() {
    const file = $('#inputCover')[0].files[0]
    const url = URL.createObjectURL(file)
    $('.article_cover').prop('src', url)
}

//获取文章分类
function getAllCategory() {

    request({
        type: "get",
        url: "http://localhost:8080/api/v1/admin/category/list",
        success: (res) => {
            // console.log(res);
            if (res.code === 200) {
                const htmlStr = template('categoryTemplate', res)
                $('#categoryId').html(htmlStr)
            }
        }
    })
}
getAllCategory()
//保存文章

function saveArticle(state) {
    let myform = new FormData($('#form')[0])
    //获取富文本内容
    const content = tinymce.activeEditor.getContent()
    myform.append('content', content)
    myform.append('state', state)
    for (let item of myform) {
        console.log(item);
    }
    request({
        type: "post",
        url: "http://localhost:8080/api/v1/admin/article/publish",
        data: myform,
        success: (res) => {
            if (res.code === 200) {
                alert(res.msg)
                window.location.href = './article_list.html'
            }
        }
    })
    //阻止表单自动跳转
    return false
}